﻿namespace Yom.WebService.Admin
{
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using Yom.Common;

    public class AdminNote : AdminBase
    {
        public void OutWrite()
        {
            string Action = Factory.Request("Action");
            string ID = Factory.Request("ID");
            string Title = Factory.Request("Title");
            string Content = Factory.Request("Content");
            string Url = Factory.Request("Url");
            string IsTop = Factory.Request("IsTop");
            StringBuilder builder;

            switch (Action)
            {
                case "Add":
                    if (string.IsNullOrEmpty(Title))
                    {
                        builder = new StringBuilder();
                        builder.Append("<fieldset class=\"note\"><dl><dd>公告标题</dd>");
                        builder.Append("<dt><input id=\"title\" type=\"text\" class=\"txt\" /></dt></dl>");
                        builder.Append("<dl><dd>公告属性</dd><dt><input type=\"checkbox\" id=\"istop\" />公告置顶&nbsp;<input type=\"checkbox\" id=\"isurl\" />外部链接<input type=\"text\" id=\"url\" class=\"txt\" style=\"display:none\" /></dt></dl>");
                        builder.Append("<dl><dd>公告内容</dd><dt>");
                        builder.Append("<input type=\"hidden\" id=\"Content\" name=\"Content\" value=\"\" />");
                        builder.Append("<iframe id=\"Content___rame\" src=\"Editor/fckeditor.html?InstanceName=Content&Toolbar=Basic\" width=\"400px\" height=\"200px\" frameborder=\"no\" scrolling=\"no\"></iframe>");
                        builder.AppendFormat("<input type=\"hidden\" id=\"Content___Config\" value=\"LinkUploadURL={0}/Service.ashx?Action%3DFileUpload%26Mod%3DEditor&ImageUploadURL={0}/Service.ashx?Action%3DFileUpload%26Mod%3DEditor&ImageUploadAllowedExtensions=.(jpg|gif|jpeg|png|bmp)$&LinkUploadAllowedExtensions=.(rar|zip|tar|gz|txt)$\" />", SiteLoad.Scheme.SiteUrl);
                        builder.Append("</dt></dl>");
                        builder.Append("</fieldset><div id=\"ErrShow\" style=\"color:red\"></div><div class=\"SubButton\"><input type=\"button\" id=\"Submit\" value=\"添加公告\" class=\"button\"/></div>");
                        base.Response.Write(builder.ToString());
                    }
                    else
                    {
                        Yom.Model.Note model = new Yom.Model.Note();
                        model.AdminID = base.Administrator.AdminID;
                        model.Content = Content;
                        model.IsTop = IsTop.ToBool();
                        model.PostIP = Factory.UserIP;
                        model.PostTime = System.DateTime.Now;
                        model.Title = Title;
                        model.Url = Url;
                        if (new Yom.Data.Note().Add(model))
                        {
                            base.Response.Write("1");
                        }
                        else
                        {
                            base.Response.Write("0");
                        }
                    }
                    break;
                case "Edit":
                    if (!string.IsNullOrEmpty(ID))
                    {
                        if (string.IsNullOrEmpty(Title))
                        {
                            Yom.Model.Note model = new Yom.Data.Note().GetModel(ID.ToInt());
                            builder = new StringBuilder();
                            builder.Append("<fieldset class=\"note\"><dl><dd>公告标题</dd>");
                            builder.Append("<dt><input id=\"title\" type=\"text\" class=\"txt\" value=\"" + model.Title + "\" /></dt></dl>");
                            builder.Append("<dl><dd>公告属性</dd><dt><input type=\"checkbox\" id=\"istop\"" + ((model.IsTop) ? "checked=\"checked\"" : "") + " />公告置顶&nbsp;<input type=\"checkbox\" id=\"isurl\" " + ((!string.IsNullOrEmpty(model.Url) ? "checked=\"checked\"" : "")) + " />外部链接<input type=\"text\" id=\"url\" class=\"txt\" " + ((string.IsNullOrEmpty(model.Url) ? "style=\"display:none\"" : "value=\"" + model.Url + "\"")) + " /></dt></dl>");
                            builder.Append("<dl><dd>公告内容</dd><dt>");
                            builder.Append("<input type=\"hidden\" id=\"Content\" name=\"Content\" value=\"" + model.Content + "\" />");
                            builder.Append("<iframe id=\"Content___Frame\" src=\"Editor/fckeditor.html?InstanceName=Content&Toolbar=Basic\" width=\"400px\" height=\"200px\" frameborder=\"no\" scrolling=\"no\"></iframe>");
                            builder.AppendFormat("<input type=\"hidden\" id=\"Content___Config\" value=\"LinkUploadURL={0}/Service.ashx?Action%3DFileUpload%26Mod%3DEditor&ImageUploadURL={0}/Service.ashx?Action%3DFileUpload%26Mod%3DEditor&ImageUploadAllowedExtensions=.(jpg|gif|jpeg|png|bmp)$&LinkUploadAllowedExtensions=.(rar|zip|tar|gz|txt)$\" />", SiteLoad.Scheme.SiteUrl);
                            builder.Append("</dt></dl>");
                            builder.Append("</fieldset><div id=\"ErrShow\" style=\"color:red\"></div><div class=\"SubButton\"><input type=\"button\" id=\"Submit\" value=\"修改公告\" class=\"button\"/></div>");
                            base.Response.Write(builder.ToString());
                        }
                        else
                        {
                            Yom.Model.Note model = new Yom.Data.Note().GetModel(ID.ToInt());
                            model.Content = Content;
                            model.IsTop = IsTop.ToBool();
                            model.PostIP = Factory.UserIP;
                            model.PostTime = System.DateTime.Now;
                            model.Title = Title;
                            model.Url = Url;
                            if (new Yom.Data.Note().Update(model))
                            {
                                base.Response.Write("1");
                            }
                            else
                            {
                                base.Response.Write("0");
                            }
                        }
                    }
                    break;
                case "Del":
                    if (!string.IsNullOrEmpty(ID))
                        if (new Yom.Data.Note().Delete(ID.ToInt()))
                            base.Response.Write("1");
                        else
                            base.Response.Write("0");
                    break;
                default:
                    builder = new StringBuilder();
                    string P = Factory.Request("P");
                    int page = (string.IsNullOrEmpty(P) ? 1 : ((P == "0") ? 1 : P.ToInt()));
                    builder.Append("<div class=\"SubMenu\"><a href=\"javascript:$.Run(3,0)\">公告管理</a> | <a href=\"javascript:$.Run(3,1)\">添加公告</a></div>");
                    builder.Append("<div align=\"left\" style=\"padding:5px;\">");
                    builder.Append("<table border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"TablePanel\"><tr align=\"center\" class=\"TDHead\">");
                    builder.Append("<td nowrap>编号</td><td nowrap>标题</td><td nowrap>外部链接</td><td nowrap>内容</td><td nowrap>发布时间</td><td nowrap>发布IP</td><td nowrap>发布者</td><td nowrap>&nbsp;</td>");
                    builder.Append("</tr>");
                    List<Yom.Model.Note> list = new Yom.Data.Note().GetList(13, page, "", "[NoteID]", false);
                    foreach (Yom.Model.Note model in list)
                    {
                        builder.Append("<tr><td align=\"center\">" + model.NoteID.ToString() + "</td><td><a href=\"javascript:$.Run(3,2," + model.NoteID.ToString() + "," + page + ")\">" + model.Title + "</a></td>");
                        builder.AppendFormat("<td align=\"left\">{0}</td>", (string.IsNullOrEmpty(model.Url) ? "&nbsp;" : model.Url));
                        builder.Append("<td align=\"left\">" + model.Content.HtmlEncode().SubStr(100, "...") + "</td>");
                        builder.Append("<td align=\"left\">" + model.PostTime.ToString("yyyy-MM-dd") + "</td>");
                        builder.Append("<td align=\"left\">" + model.PostIP + "</td>");
                        builder.Append("<td align=\"center\">" + new Yom.Data.Admin().GetModel(model.AdminID).UserName + "</td>");
                        builder.Append("<td align=\"center\"><img border=\"0\" src=\"Images/Admin/del.gif\" alt=\"删除公告\" width=\"16\" height=\"16\" onclick=\"$.Run(3,3," + model.NoteID.ToString() + "," + page.ToString() + ")\" style=\"cursor:pointer\" /></td></tr>");
                    }
                    builder.Append("</table><br /><br /><div id=\"ShowMsg\" style=\"color:red\"></div>");
                    builder.Append("<div class=\"more\">" + Factory.GetPager(13, page, new Yom.Data.Note().GetCount(), "javascript:$.Run(3,0,<{Page}>)") + "</div>");
                    base.Response.Write(builder.ToString());
                    break;
            }
        }
    }
}